#include <soar.h>

int main()
{
    char *desc;
    char msg[BUFFER_SIZE];
    char buf[BUFFER_SIZE];
    PARA para;
    int res;
    int i;
    int num;
    char *name, *value;

    desc=
        "option(number:1)                   "
        "type(string:4)                     "
        "user_info(array)                   "
        "{                                  "
        "    id(number:6)                   "
        "    name(string:1-10)              "
        "    item_info(array)               "
        "    {                              "
        "        id(number:12)              "
        "        name(string:3-10)          "
        "    }                              "
        "    login_num(number:1-6)          "
        "}                                  "
        "note(string:1-1000)                "
        ;

    CLR_BUF(msg);
    para_put_item(msg, "1");                // option
    para_put_item(msg, "TEST");             // type
    para_put_item_int(msg, 2);     // user_info
    {
        para_put_item_short(msg, 1);        // id
        para_put_item(msg, "A1");           // name
        para_put_item_int(msg, 2); // item_info
        {
            para_put_item_long(msg, 1L);    // id
            para_put_item(msg, "ITEM1");    // name
            para_put_item_long(msg, 2L);
            para_put_item(msg, "ITEM2");
        }
        para_put_item_short(msg, 8);        // login_num

        para_put_item_short(msg, 2);
        para_put_item(msg, "A2");
        para_put_item(msg, "2");  // array
        {
            para_put_item_long(msg, 1L);
            para_put_item(msg, "ITEM3");
            para_put_item_long(msg, 2L);
            para_put_item(msg, "ITEM4");
        }
        para_put_item(msg, "-100");   // number
    }
    para_put_item(msg, "备注");

    //while(1)
    {
        if((res=para_load(msg, desc, &para)))
        {
            printf("para_load fail, res:%d\n", res);
            return(-1);
        }

        num=para_num(para);
        for(i=0; i<num; i++)
        {
            if((res=para_ref_by_index(para, i, &name, &value)))
            {
                printf("para_ref_by_index fail, res:%d\n", res);
            }
            printf("%d(%s): %s\n", i, name, value); 
        }

        name="option";
        if((res=para_ref_by_name(para, name, &value)))
        {
            printf("para_ref_by_name(%s) fail, res:%d\n", name, res);
            return(-1);
        }
        printf("%s: %s\n", name, value);

        name="user_info";
        if((res=para_ref_by_name(para, name, &value)))
        {
            printf("para_ref_by_name(%s) fail, res:%d\n", name, res);
            return(-1);
        }
        printf("%s: %s\n", name, value);

        name="user_info[0].id";
        if((res=para_ref_by_name(para, name, &value)))
        {
            printf("para_ref_by_name(%s) fail, res:%d\n", name, res);
            return(-1);
        }
        printf("%s: %s\n", name, value);

        name="user_info[1].item_info[1].name";
        if((res=para_ref_by_name(para, name, &value)))
        {
            printf("para_ref_by_name(%s) fail, res:%d\n", name, res);
            return(-1);
        }
        printf("%s: %s\n", name, value);

        name="user_info[1].item_info[-1].name";
        if((res=para_ref_by_name(para, name, &value)))
        {
            printf("para_ref_by_name(%s) fail, res:%d\n", name, res);
            return(-1);
        }
        printf("%s: %s\n", name, value);
        name="user_info[1].item_info[-1].name";
        if((res=para_copy_by_name(para, name, buf)))
        {
            printf("para_copy_by_name(%s) fail, res:%d\n", name, res);
            return(-1);
        }
        printf("%s: %s(copy)\n", name, buf);

        name="user_info[1].item_info[10].id";
        if((res=para_ref_by_name(para, name, &value)))
            printf("para_ref_by_name(%s) fail, res:%d\n", name, res);
        else
            printf("%s: %s\n", name, value);

        name="user_info[1].item_info[0].xname";
        if((res=para_ref_by_name(para, name, &value)))
            printf("para_ref_by_name(%s) fail, res:%d\n", name, res);
        else
            printf("%s: %s\n", name, value);

        name="user_info[1].item_info";
        if((res=para_ref_by_name(para, name, &value)))
            printf("para_ref_by_name(%s) fail, res:%d\n", name, res);
        else
            printf("%s: %s\n", name, value);

        name="user_info[1].item_info[0].";
        if((res=para_ref_by_name(para, name, &value)))
            printf("para_ref_by_name(%s) fail, res:%d\n", name, res);
        else
            printf("%s: %s\n", name, value);

        para_free(para);
    }

    printf("Succeed\n");

    return(0);
}
